package com.quanshu.change.login.view;

import android.content.Context;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.LoginInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.CountDownTimerUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ResetPasswordActivity extends BaseActivity {

    @BindView(R.id.reset_pwd_title)
    BaseTitleBar resetPwdTitle;
    @BindView(R.id.reset_pwd_title_txt)
    TextView resetPwdTitleTxt;
    @BindView(R.id.send_message_button)
    TextView sendMessageButton;
    @BindView(R.id.reset_pwd_get_code)
    FrameLayout resetPwdGetCode;
    @BindView(R.id.reset_pwd_input_code)
    EditText resetPwdInputCode;
    @BindView(R.id.reset_pwd_cl)
    ConstraintLayout resetPwdCl;
    @BindView(R.id.alter_pwd)
    TextView alterPwd;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);
        mContext = this;
        initView();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(resetPwdTitle).statusBarColor("#ffffff").statusBarDarkFont(true).init();
        resetPwdTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("重置登录密码").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        resetPwdInputCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() >= 4) {
                    alterPwd.setEnabled(true);
                } else {
                    alterPwd.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        RxViewHelper.getInstance().clicks(resetPwdGetCode, aVoid -> doSendSms());
        RxViewHelper.getInstance().clicks(alterPwd, aVoid -> resetPwd());
    }

    /**
     * 发送短信验证码
     */
    private void doSendSms() {

        NetClient.getInstance()
                .getShopService(LoginInterface.class)
                .doSendSms(AppUtils.getSignParams(MyApplication.getInstance().getCurrentUser().getMobile()), MyApplication.getInstance().getCurrentUser().getMobile())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        ToastHelper.toast("验证码已发送至" + MyApplication.getInstance().getCurrentUser().getMemberid());
                        CountDownTimerUtils timerUtils = new CountDownTimerUtils(sendMessageButton, 60000, 1000);
                        timerUtils.start();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);
                    }
                });
    }

    /**
     * 重置登录密码
     */

    private void resetPwd() {
        NetClient.getInstance()
                .getShopService(LoginInterface.class)
                .alterLoginPwd(AppUtils.getSignParams(MyApplication.getInstance().getCurrentUser().getMobile(), resetPwdInputCode.getText().toString()), MyApplication.getInstance().getCurrentUser().getMobile(), resetPwdInputCode.getText().toString())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        Bundle bundle=new Bundle();
                        bundle.putInt(ResetPasswordSuccessfulActivity.TIPS_SHOW,1);
                        BaseTool.goActivity(mContext, ResetPasswordSuccessfulActivity.class, bundle);
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        super.onFailure(e);
                    }
                });
    }

}
